package com.rtx.edi.platform.synctask.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rtx.edi.model.api.po.PlApiMapping;
import com.rtx.edi.platform.baseinfo.service.PlApiService;
import com.rtx.edi.platform.synctask.api.GatWayAPI;
import com.rtx.edi.platform.synctask.service.GatWayService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"网关"})
@RestController
@RequestMapping("/edi/")
public class GatWayController {
	
	@Resource(name = "ApiGatWayService")
	GatWayService gatWayService;
	
	@Autowired
	ApplicationContext context;
	
	@Autowired
	private PlApiService plApiService;

	
	@ApiOperation(value = "同步请求", notes = "同步请求")
	@PostMapping("gatWay/std")
	public String syncRequest(HttpServletRequest request) throws Exception {
		String method = request.getParameter("method");

		PlApiMapping plApiMapping = plApiService.getPlApiMappingByApiMappingCode(method);
		GatWayAPI gatWayAPI = null;
		if(StringUtils.hasLength(plApiMapping.getActivegatway())) {
			gatWayAPI = (GatWayAPI) context.getBean(plApiMapping.getActivegatway());
		}else {
			gatWayAPI = gatWayService;
		}
		return gatWayAPI.handel(request);
	}
	
	
	@ApiOperation(value = "异步请求", notes = "异步请求")
	@PostMapping("gatWay/std/asynchronous")
	public String asynchronousRequest(HttpServletRequest request) throws Exception {
		return gatWayService.asynchronousHandel(request);
	}
}
